﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using Cesave.DistributedServices.Core.ErrorHandlers;
using Cesave.DistributedServices.PermisosModule.Resources;
using Cesave.Domain.PermisosModule.Contracts.Acls;
using Cesave.Domain.PermisosModule.Entities;
using Cesave.Infrastructure.CrossCutting.IoC;
using Cesave.Infrastructure.CrossCutting.Logging;

namespace Cesave.DistributedServices.PermisosModule
{
	public partial class PermisosModuleService
	{
		#region IPermisosModuleService Members

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public List<Acl> FindAllAcl()
		{
			try
			{
				IAclService aclService = IoCFactory.Resolve<IAclService>();
				return aclService.FindAllAcl();
			}
			catch (Exception ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="aclGuid"></param>
		/// <returns></returns>
		public Acl FindAclByGuid(Guid aclGuid)
		{
			try
			{
				IAclService aclService = IoCFactory.Resolve<IAclService>();
				return aclService.FindAclByGuid(aclGuid);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
			catch (ArgumentNullException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="acl"></param>
		public void AddAcl(Acl acl)
		{
			try
			{
				IAclService aclService = IoCFactory.Resolve<IAclService>();
				aclService.AddAcl(acl);
			}
			catch (ArgumentNullException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="acl"></param>
		public void ChangeAcl(Acl acl)
		{
			try
			{
				IAclService aclService = IoCFactory.Resolve<IAclService>();
				aclService.ChangeAcl(acl);
			}
			catch (ArgumentNullException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="acl"></param>
		public void DeleteAcl(Acl acl)
		{
			try
			{
				IAclService aclService = IoCFactory.Resolve<IAclService>();
				aclService.DeleteAcl(acl);
			}
			catch (ArgumentNullException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		#endregion
	}
}
